<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,
        body {
            background-color: #f2f2fc;
            height: 100vh;
            overflow: hidden;
        }

        * {
            margin: 0;
            padding: 0;
            list-style: none;
            text-decoration: none;
            user-select: none;
        }

        ::-webkit-scrollbar {
            display: none;
        }

        .contentBox {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin-top: 40px;
            gap: 10px;
        }

        .inputBox {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .inputBox>input {
            outline: none;
            border: 2px solid rgba(128, 128, 128, 0.3);
            border-radius: 6px;
            height: 30px;
            width: 320px;
            padding: 0 10px;
        }

        .inputBox>p {
            width: 30px;
            height: 30px;
            font-size: 24px;
            font-weight: 500;
            background-color: #fff;
            color: gray;
            border-radius: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid rgba(128, 128, 128, 0.3);
            cursor: pointer;
        }

        .inputBox>input:focus {
            border: 2px solid orangered;
        }

        ul {
            display: flex;
            flex-direction: column;
            gap: 10px;
            align-items: center;
            overflow-y: auto;
            height: 80vh;
        }

        ul>li {
            display: flex;
            gap: 5px;
            align-items: center;
        }

        ul>li>p {
            background-color: #fff;
            border-radius: 6px;
            padding: 0 10px;
            max-width: 320px;
            word-wrap: break-word;
            overflow-wrap: break-word;
        }

        ul>li>span {
            width: 30px;
            height: 30px;
            font-size: 16px;
            font-weight: 500;
            background-color: #fff;
            color: gray;
            border-radius: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid rgba(128, 128, 128, 0.3);
            cursor: pointer;
        }

        ul>li>span:hover {
            filter: brightness(0.9);
            color: orangered;
        }

        ul>li>div {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        ul>li>div>input {
            outline: none;
            border: 2px solid rgba(128, 128, 128, 0.3);
            border-radius: 6px;
            height: 30px;
            width: 200px;
            padding: 0 10px;
        }

        ul>li>div>input:focus {
            border: 2px solid orangered;
        }

        ul>li>div>span {
            height: 30px;
            padding: 0 5px;
            line-height: 30px;
            text-align: center;
            color: #fff;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        ul>li>div>span:nth-of-type(1) {
            background-color: #16b777;
        }

        ul>li>div>span:nth-of-type(2) {
            background-color: brown;
        }
    </style>
</head>

<body>
    <div class="contentBox">
        <h3>待办事项</h3>
        <span class="inputBox">
            <input type="text">
            <p>+</p>
        </span>
        <ul></ul>
    </div>

    <script type="module">
        import { getTodoList, addTodo, delTodo, modifyTodo } from './api.js';

        function $(ele) {
            return document.querySelector(ele);
        }

        const ipt = $('input');
        const subBtn = $('.inputBox>p');
        const ul = $('ul');

        renderPage();

        function renderPage() {
            getTodoList()
                .then(data => {
                    let dt = data;
                    console.log(dt);
                    ul.innerHTML = dt.reduce((total, item) => {
                        return total += `<li id=${item.id}><input type="checkbox"><p>${item.title}</p><span>×</span></li>`;
                    }, '');
                })
                .catch(err => {
                    console.error('获取数据失败:', err);
                });
        }

        subBtn.onclick = function () {
            let title = ipt.value;
            addTodo(title)
                .then(data => {
                    console.log(data);
                })
                .catch(err => {
                    console.error('获取数据失败:', err);
                });
            renderPage();
            ipt.value = '';
        };

        ul.onclick = async function (e) {
            const deleteBtn = e.target.closest('li > span');
            if (deleteBtn?.textContent.trim() === '×') {
                const id = deleteBtn.parentNode.id;
                try {
                    await delTodo(id);
                    renderPage();
                } catch (err) {
                    console.error('删除失败:', err);
                }
            }

            const selectBtn = e.target.closest('li>input');

            if (selectBtn?.type === 'checkbox') {
                let selectBtns = document.querySelectorAll('li>input[type="checkbox"]');
                selectBtns.forEach(function (item) {
                    if (item !== selectBtn) {
                        item.checked = false;
                    }
                    let oDiv = item.parentNode.querySelector('div');
                    if (oDiv) {
                        oDiv.remove();
                    }
                });

                let id = selectBtn.parentNode.id;
                if (selectBtn.checked) {
                    let newDiv = document.createElement('div');
                    newDiv.innerHTML = `<input type="text"> 
                                        <span>确认</span>
                                        <span>取消</span>`;
                    selectBtn.parentNode.appendChild(newDiv);
                } else {
                    let oDiv = selectBtn.parentNode.querySelector('div');
                    if (oDiv) {
                        oDiv.remove();
                    }
                }
            }

            const okBtn = $('li>div>span:nth-of-type(1)');
            const noBtn = $('li>div>span:nth-of-type(2)');
            if (okBtn) {
                let title = okBtn.previousElementSibling.value;
                let id = okBtn.parentNode.parentNode.id;
                if (title.trim() !== '') {
                    console.log(id);
                    modifyTodo(id, title)
                        .then(data => {
                            console.log(data);
                        })
                        .catch(err => {
                            console.error('修改数据失败:', err);
                        });
                    renderPage();
                }
            }
            if (noBtn) {
                noBtn.onclick = function () {
                    this.parentNode.parentNode.querySelector('input[type="checkbox"]').checked = false;
                    this.parentNode.remove();
                };
            }
        };
    </script>
</body>

</html>